<?php
/**
 * Group joining, leaving, administrating, and messaging
 *
 * ACCESS: LOGGED IN USERS ONLY
 *
 * @author Chris Lunsford
 * @version 1.0
 */
class Groups extends Controller {
	
	/**
	 * Class constructor
	 */
	function __construct() {
		parent::__construct();
		
		$this->badger->checkState();
		
		$this->load->model('UserModel');
	}
	
	/******************** URL Functions ********************/
	/**
	 * Display a list of all groups that a member is a part of, with basic info about each group
	 * and links to useful functions for each group
	 */
	function index() {
		$data = $this->_buildContent();
		$assigns = $this->UserModel->getUserAssigns($data['con_user']->userid);
		
		if ($assigns != FALSE) {
			foreach ($assigns->result() as $row) {
				$group = $this->UserModel->getGroup($row->groupid);
				$row->group_members = $this->UserModel->getGroupMembers($row->groupid);
				$row->description = $group->description;
			}
			$data['groups'] = $assigns;
		} else {
			$data['groups'] = FALSE;
		}		
		$this->load->view('groups/my_groups',$data);
	}
	
	
	/******************* Task Functions *******************/
	
	/*
	 * Supplies user-specific information to the interface for customization
	 *
	 */
	function _buildContent() {
		//REQUIRED//
		$data['sidebar'] = TRUE;
		if ($this->session->flashdata('msg_info')) $data['msg_info'] = $this->session->flashdata('msg_info');
		if ($this->session->flashdata('msg_error')) $data['msg_error'] = $this->session->flashdata('msg_error');
		
		//test if user is logged in for site navigation
		if ($this->session->userdata('loggedin')) {
			$data['loggedin'] = TRUE;
			
			//grab the user
			$user = $this->UserModel->getUserByName($this->session->userdata('username'));
			//test for identity swap mystery
			log_message('info','Generated sidebar content. Username: '.$user->username.' Session: '.$this->session->userdata('username').'.');
		
			$data['con_user'] = $user;
			
			//is admin?
			if ($this->UserModel->isUserInGroup($user->userid,$this->UserModel->getGroupID('admin'))) {
				$data['admin_tab'] = TRUE;
			}
		}
					
		return $data;
	}
}
?>